package cn.pb.news3.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cn.pb.news3.util.ConfigManager;
// 操作数据库的基类
public class BaseDao {
	protected Connection connection;
	protected ResultSet rs;
	protected PreparedStatement pst;
	protected String sql;
	
	// 获取数据库连接
	public boolean getConnection(String sql){
		String driver = ConfigManager .getInstance().getString("jdbc.driver.class");
		String url = ConfigManager .getInstance().getString("jdbc.connection.url");
		String username = ConfigManager .getInstance().getString("jdbc.connection.username");
		String password = ConfigManager .getInstance().getString("jdbc.connection.password");
		try {
			// 1、加载驱动
			Class.forName(driver);
			// 2、获取数据库连接
			connection = DriverManager.getConnection(url, username, password);
			// 获取prepareStatement对象
			pst = connection.prepareStatement(sql);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	// 通过数据源获取连接
	public Connection getConnectionDs(){
		try {
			// 初始化上下文对象
			Context context = new InitialContext();
			// 获取数据源
			DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/news");
			// 获取connection
			connection = ds.getConnection();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
		
	}
	
	//查询操作
	public ResultSet executeQuery(String sql, Object[] params){
		if(getConnection(sql) == true){
			for(int i = 0; i < params.length; i++){
				try {
					//添加占位符（动态置参）
					pst.setObject(i+1, params[i]);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				rs = pst.executeQuery();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rs;
	}
	
	// 增删改操作
	public int executeUpdate(String sql, Object[] params){
		int updateRows = 0;
		if(getConnection(sql) == true){
			for(int i = 0; i < params.length; i++){
				try {
					// 添加占位符（动态置参）
					pst.setObject(i+1, params[i]);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				updateRows = pst.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return updateRows;
	}
	
	//释放资源
	public boolean close(){
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		if(pst != null){
			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		if(connection != null){
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

}
